package com.ninetaildemonfox.zdl.socialproject.mvp.ui.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.BottomMenu;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.kongzue.takephoto.TakePhotoUtil;
import com.ninetaildemonfox.zdl.ninetaildemonfox.base.BaseFragment;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.data.entity.Event2Message;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.MyContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.MyPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.AuthenticationCenterActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.BindingInvitationCodeActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.CollectionActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DynameicActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.EditActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.EditorialMaterialsActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.InvitationActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MainActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MyDateActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MyWalletActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.PicturePreviewActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.RegionalAgentActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.SetUpActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.VideoActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.VipActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.ImageViewAdp3;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.RoundImageView;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.FlowDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/15 15:10
 * 功能描述： 我的界面
 * 联系方式：1037438704@qq.com
 */
public class MyFragment extends BaseFragment implements MyContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_up)
    TextView text_up;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_burn_down_count)
    TextView textBurnDownCount;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.text_my_invitation)
    TextView textMyInvitation;
    @BindView(R.id.text_regional_agent)
    TextView textRegionalAgent;
    @BindView(R.id.ll_my_authentication)
    LinearLayout llMyAuthentication;
    @BindView(R.id.ll_vip_view)
    LinearLayout llVipView;
    @BindView(R.id.text_my_wallet)
    TextView textMyWallet;
    @BindView(R.id.image_head)
    RoundImageView imageHead;
    @BindView(R.id.text_invitation_me)
    TextView textInvitationMe;
    @BindView(R.id.ll_set_up)
    LinearLayout llSetUp;
    @BindView(R.id.ll_my_date)
    LinearLayout llMyDate;
    @BindView(R.id.ll_collection)
    LinearLayout llCollection;
    @BindView(R.id.ll_recovery)
    LinearLayout llRecovery;
    @BindView(R.id.ll_dynamic)
    LinearLayout llDynamic;
    @BindView(R.id.ll_evaluate)
    LinearLayout llEvaluate;
    @BindView(R.id.text_burn_down)
    TextView textBurnDown;
    @BindView(R.id.text_age)
    TextView textAge;
    private MyPresenter myPresenter;
    @BindView(R.id.text_city)
    TextView textCity;
    @BindView(R.id.text_identification)
    TextView textIdentification;
    @BindView(R.id.text_renzheng)
    TextView text_renzheng;
    private Drawable drawable;
    @BindView(R.id.recycler_album)
    RecyclerView recyclerAlbum;
    @BindView(R.id.text_vip_time)
    TextView textVipTime;
    private ImageViewAdp3 imageViewAdp;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my;
    }


    @Override
    public void onInitView(Bundle savedInstanceState) {
        textRight.setText("编辑资料");
        textRight.setVisibility(View.VISIBLE);
        myPresenter = new MyPresenter(this);
        recyclerAlbum.setLayoutManager(new GridLayoutManager(me, 4));
        imageViewAdp = new ImageViewAdp3(R.layout.imageview3);
        recyclerAlbum.setAdapter(imageViewAdp);
        //1 是boy  2 女孩
        int sex = Integer.valueOf(Preferences.getInstance().getString(getContext(), "user", "sex"));
        if (sex == 1) {
            llVipView.setVisibility(View.VISIBLE);
            llMyAuthentication.setVisibility(View.GONE);
        } else {
            llMyAuthentication.setVisibility(View.VISIBLE);
            llVipView.setVisibility(View.GONE);
        }
        imageViewAdp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = imageViewAdp.getData();
                PicturePreviewActivity.start(getActivity(), data, position, 1, "");
            }
        });
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event2Message event) {
        if (event == null) {
            return;
        }
        WaitDialog.show(fgtContext, "");
        HttpRequest.POST(me, "Member/uploadImgLook"
                , new Parameter()
                        .add("token", Preferences.getInstance().getString(me, "user", "token"))
                        .add("img", event.getFile())//上传图片
                        .add("is_see", event.getIs_see())//设置阅后即焚 1否 2是
                        .add("type", event.getType())//1上传 2设置
                        .add("id", event.getPicId())  // 预览 设置是否焚毁 相册id【设置时图片或视频 id】
                        .add("file_type", event.getFile_type())//1视频 2 图片
                        .add("video", event.getFileVideo())//上传视频
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        WaitDialog.dismiss();
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            if (map.get("code").equals("1")) {
                                ToastUtil.show("上传成功");
                                myPresenter.getMemberBaseData(token, fgtContext);
                            } else {
                                ToastUtil.show("上传失败");
                            }
                        } else {
                            ToastUtil.show("由于网络原因上传失败");
                        }
                    }
                });
    }


    @Override
    public void onResume() {
        super.onResume();
        getIdentification = Preferences.getInstance().getString(me, "user", "identification");
        getVip = Preferences.getInstance().getString(me, "user", "vip");
        WaitDialog.show(fgtContext, "");
        myPresenter.getMemberBaseData(Preferences.getInstance().getString(me, "user", "token"), fgtContext);
    }

    @OnClick({R.id.text_my_wallet, R.id.ll_vip_view, R.id.image_head, R.id.text_right
            , R.id.ll_my_authentication, R.id.text_my_invitation, R.id.text_regional_agent,
            R.id.ll_set_up, R.id.text_invitation_me, R.id.text_burn_down, R.id.ll_my_date
            , R.id.ll_collection, R.id.ll_dynamic, R.id.ll_evaluate, R.id.text_up})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_my_wallet:
                //我的钱包
                startActivity(new Intent(getContext(), MyWalletActivity.class));
                break;
            case R.id.ll_vip_view:
                //会员中心
//                vip_time
                VipActivity.start(getContext(), myVip, vip_time);
                break;
            case R.id.image_head:
                //点击头像
                startActivity(new Intent(getContext(), EditActivity.class));
                break;
            case R.id.text_right:
                //编辑资料
                EditorialMaterialsActivity.start(getContext(), 2);
                break;
            case R.id.ll_my_authentication:
                //认证界面
                List<Map<String, String>> data = imageViewAdp.getData();
                Intent intent = new Intent(getContext(), AuthenticationCenterActivity.class);
                startActivity(intent);
                break;
            case R.id.text_my_invitation:
                //邀请好友
                startActivity(new Intent(getContext(), InvitationActivity.class));
                break;
            case R.id.text_regional_agent:
//                是否vip 1否 2是           1未认证 2 已认证
                if (getSex.equals("1")) {
                    if (myVip.equals("1")) {
                        ToastUtil.show("需要成为VIP");
                        return;
                    }
                } else {
                    if (identification.equals("1")) {
                        ToastUtil.show("认证后才会开启区域代理");
                        return;
                    }
                }
                //申请代理
                startActivity(new Intent(getContext(), RegionalAgentActivity.class));
                break;
            case R.id.text_invitation_me:
                //绑定邀请码  谁邀请我
                startActivity(new Intent(getContext(), BindingInvitationCodeActivity.class));
                break;
            case R.id.ll_set_up:
                //设置界面
                startActivity(new Intent(getContext(), SetUpActivity.class));
                break;
            case R.id.text_burn_down:
                //设置界面
                MessageDialog.show(fgtContext, "", "确定要恢复已被用户焚毁的照片吗？", "确认", "取消")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                myPresenter.replyImage(token, fgtContext);
                                return false;
                            }
                        });

//                DialogShow.start(getContext(), "5");
                break;
            case R.id.ll_my_date:
                //我的约会
                MyDateActivity.start(getContext(), id);
                break;
            case R.id.ll_collection:
                //我的收藏
                startActivity(new Intent(getContext(), CollectionActivity.class));
                break;
            case R.id.ll_dynamic:
                //我的动态
                if (id == null) {
                    return;
                }
                DynameicActivity.start(getContext(), 2, id, "0");
                break;
            case R.id.ll_evaluate:
                //我的评价
                FlowDialog.start(getContext(), id, "1", "");
                break;
            case R.id.text_up:
                BottomMenu.show(fgtContext, new String[]{"视频", "从相册里选择"}, new OnMenuItemClickListener() {
                    @Override
                    public void onClick(String text, int index) {
                        switch (index) {
                            case 0:
                                startActivity(new Intent(me, VideoActivity.class));
                                break;
                            case 1:
//                                调用相册
                                MainActivity.thisMainCount = 1;
                                TakePhotoUtil.getInstance(fgtContext).doOpenGallery();
                                break;
//                            case 2:
//                                //使用相机拍摄
//                                TakePhotoUtil.getInstance(fgtContext).doOpenCamera();
//                                break;
                            default:
                        }
                    }
                });
                break;
            default:
        }
    }

    private String myVip;
    private String id;
    String vip_time;
    private String identification;

    @Override
    public void MySuccess(Map<String, String> data) {
        vip_time = data.get("vip_time");
        textCenter.setText(data.get("nickname"));
        Preferences.getInstance().commit(me, "user", "nickname", data.get("nickname"));
        textAge.setText(data.get("age"));
        GlideUtils.glideFille(me, data.get("head_pic"), imageHead);
        id = data.get("id");
        Preferences.getInstance().commit(me, "user", "id", id);
        myVip = data.get("vip");//是否是vip  1否 2是
        Preferences.getInstance().commit(me, "user", "vip", myVip);
        //城市
        textCity.setText(data.get("city_name"));
        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(data.get("pics"));
        Preferences.getInstance().set(fgtContext, "user", "dataSize", list.size());
        if (list != null) {
            imageViewAdp.setNewData(list);
            recyclerAlbum.setVisibility(View.VISIBLE);
            //阅后即焚  显示隐藏
            llRecovery.setVisibility(View.VISIBLE);
            textBurnDownCount.setText("已有" + data.get("see") + "个人焚毁了你的照片");
        } else {
            recyclerAlbum.setVisibility(View.GONE);
            //阅后即焚  显示隐藏
            llRecovery.setVisibility(View.GONE);
        }
        // 是否认证 1否 2是
        identification = data.get("identification");
        Preferences.getInstance().commit(me, "user", "identification", identification);
        String sex = data.get("sex");//性别  1男 2女
        if (sex.equals("1")) {
//            ic_boy_sex
            drawable = mContext.getResources().getDrawable(R.mipmap.icon_boy_my);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            // 是否vip 1否 2是
            if (myVip.equals("1")) {
                textIdentification.setText("非会员");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
                textVipTime.setText("点击购买会员");
            } else {
                textIdentification.setText("会员");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
                // vip 到期时间
                textVipTime.setText("过期时间" + data.get("vip_time"));
            }
        } else if (sex.equals("2")) {
            drawable = mContext.getResources().getDrawable(R.mipmap.ic_girl_sex);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            if (identification.equals("1")) {
                textIdentification.setText("您还没有认证身份的真实性");
                text_renzheng.setText("您还没有认证身份的真实性");
            } else if (identification.equals("2")) {
                textIdentification.setText("已认证");
                text_renzheng.setText("已认证");
            }

        }
        textAge.setCompoundDrawables(drawable, null, null, null);
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void recovery() {
        myPresenter.getMemberBaseData(Preferences.getInstance().getString(me, "user", "token"), fgtContext);
    }

}
